<?php
require_once 'db_config.php';
define("DEBUGDB",true);

class BaseDatos
{
    
    function __construct($host, $usuario, $clave, $base) {
        $this->conexion = @mysql_connect($host, $usuario, $clave);
        if(!$this->conexion || !mysql_select_db($base, $this->conexion))
        {
            if(DEBUGDB){
                print mysql_error();
                exit;
            }
        }
        
    }
    
    function __destruct() {
        if($this->conexion)
            mysql_close($this->conexion);
    }
    
    function selectAsociativo($sql)
    {
        $result = mysql_query($sql);

        if (!$result) {
            if(DEBUGDB) print "Could not successfully run query ($sql) from DB: " . mysql_error();
            exit;
        }

        if (mysql_num_rows($result) == 0)
            return false;

        $retval = array();
        $i = 0;
        while ($row = mysql_fetch_assoc($result))
        {
            foreach($row as $sub => $val)
            {
                $retval[$i][$sub] = is_numeric($val)?$val:stripslashes($val);
            }
            $i++;
        }
        return $retval;
    }
    
    function selectUnico($sql)
    {
        $result = mysql_query($sql);

        if (!$result) {
            if(DEBUGDB) print "Could not successfully run query ($sql) from DB: " . mysql_error();
            exit;
        }

        if (mysql_num_rows($result) == 0)
            return false;
        return mysql_result($result,0);
    }
    
    function insertUpdate($sql)
    {
        $result = mysql_query($sql);

        if (!$result) {
            if(DEBUGDB) print "Could not successfully run query ($sql) from DB: " . mysql_error();
            return false;
        }
        return true;
    }
}

?>
